package ${servicePackage}.impl;


import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;


import ${queryPackage}.${queryModel?cap_first};
import ${daoPackage}.${modular}.db.dao.${tableNameHump?cap_first}Mapper;
import ${daoPackage}.${modular}.db.domain.${tableNameHump?cap_first};
import ${daoPackage}.${modular}.db.domain.${tableNameHump?cap_first}.Column;
import ${daoPackage}.${modular}.db.domain.${tableNameHump?cap_first}Example;
import ${servicePackage}.I${tableNameHump?cap_first}SV;
import com.github.pagehelper.PageHelper;

@Service("${tableNameHump}SV")
public class ${tableNameHump?cap_first}SVImpl implements I${tableNameHump?cap_first}SV
{
	@Resource
	private ${tableNameHump?cap_first}Mapper ${tableNameHump}Mapper;
	
	private static final Column[] columns = new Column[] { <#list columns as column>Column.${column.humpName}<#if column_has_next>, </#if></#list> };
	
	/**
	 * 搜索${description}
	 <#list searchs as search>
	 * @param   ${search.humpName} ${search.javaType} ${search.description} 
	 </#list>
	 * @return List<${tableNameHump?cap_first}>
	 * @throws Exception
	 */
	@Override
	public List<${tableNameHump?cap_first}> queryList(${queryModel?cap_first} ${queryModel?uncap_first} ,int offset, int limit, String sort, String order){
	
	
		${tableNameHump?cap_first}Example example = new ${tableNameHump?cap_first}Example();
		${tableNameHump?cap_first}Example.Criteria criteria =  example.createCriteria() ;
		criteria.andDeletedEqualTo(false);
		
	<#list searchs as search>
		<#if search.javaType == "String" >
		if (StringUtils.isNotBlank(${queryModel?uncap_first}.get${search.humpName?cap_first}())) {
			criteria.and${search.humpName?cap_first }EqualTo(${queryModel?uncap_first}.get${search.humpName?cap_first}()) ;
		}
		<#else>
		if (null != ${queryModel?uncap_first}.get${search.humpName?cap_first}()) {
			criteria.and${search.humpName?cap_first }EqualTo(${queryModel?uncap_first}.get${search.humpName?cap_first}()) ;
		}
		</#if>
	</#list> 
		
		
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			example.setOrderByClause(sort + " " + order);
		}
		PageHelper.startPage(offset, limit);
		return ${tableNameHump}Mapper.selectByExampleSelective(example, columns);
	
	}

	/**
	 * 搜索${description}
	 <#list searchs as search>
	 * @param   ${search.humpName} ${search.javaType} ${search.description} 
	 </#list>
	 * @return int
	 * @throws Exception
	 */
	@Override
	public long queryTotal(${queryModel?cap_first}  ${queryModel?uncap_first}){
			${tableNameHump?cap_first}Example example = new ${tableNameHump?cap_first}Example();
			${tableNameHump?cap_first}Example.Criteria criteria =  example.createCriteria() ;
			criteria.andDeletedEqualTo(false);
		
		<#list searchs as search>
			<#if search.javaType == "String" >
			if (StringUtils.isNotBlank(${queryModel?uncap_first}.get${search.humpName?cap_first}())) {
				criteria.and${search.humpName?cap_first }EqualTo(${queryModel?uncap_first}.get${search.humpName?cap_first}()) ;
			}
			<#else>
			if (null != ${queryModel?uncap_first}.get${search.humpName?cap_first}()) {
				criteria.and${search.humpName?cap_first }EqualTo(${queryModel?uncap_first}.get${search.humpName?cap_first}()) ;
			}
			</#if>
		</#list> 
			
			return ${tableNameHump}Mapper.countByExample(example);
	}
	
	
	/**
	 * 根据Id获取${description}
	 * @param id
	 * @return
	 */
	@Override
	public ${tableNameHump?cap_first} findById(long id){
		return ${tableNameHump}Mapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 新增${description}
	 * @param ${tableNameHump?cap_first}
	 * @return
	 */
	@Override
	public ${tableNameHump?cap_first} add(${tableNameHump?cap_first} ${tableNameHump}){
	
			if (null ==  ${tableNameHump}) {
				return null ;
			}
	
	<#list columns as row>
		<#if  row.name == "id" >
			${tableNameHump}.set${row.humpName?cap_first}(null);
		<#elseif row.name == "ctime" ||  row.name == "utime">
			${tableNameHump}.set${row.humpName?cap_first}(java.time.LocalDateTime.now()) ;
		<#else>
			<#if row.javaType == "String" >
			if (StringUtils.isNotBlank( ${tableNameHump}.get${row.humpName?cap_first}())) {
				${tableNameHump}.set${row.humpName?cap_first}(${tableNameHump}.get${row.humpName?cap_first}().trim());
			}else{
				${tableNameHump}.set${row.humpName?cap_first}("");
			}
			<#else>
			if (null != ${tableNameHump}.get${row.humpName?cap_first}()) {
				${tableNameHump}.set${row.humpName?cap_first}(${tableNameHump}.get${row.humpName?cap_first}());
			}
			</#if>
		</#if>
	</#list> 
		
			${tableNameHump}Mapper.insertSelective(${tableNameHump});
			return  ${tableNameHump} ;
	}
	
	/**
	 * 根据更新${description}
	 * @param ${tableNameHump}
	 */
	@Override
	public int updateById(${tableNameHump?cap_first} ${tableNameHump}){
	
		if (null == ${tableNameHump} || null == ${tableNameHump}.getId()) {
			return 0;
		}

		${tableNameHump?cap_first} original =  ${tableNameHump}Mapper.selectByPrimaryKey(${tableNameHump}.getId());
		if (null == original) {
			return 0;
		}
		
		return ${tableNameHump}Mapper.updateByPrimaryKeySelective(${tableNameHump});
	}
	
	/**
	 * 删除${description}
	 * @param id
	 * @throws Exception
	 */
	@Override
	public void delete(long id){
		${tableNameHump}Mapper.logicalDeleteByPrimaryKey(id) ;
	}
}
